package com.lovecanon.jzjl.common;

import java.io.Serializable;

/**
 * 
 * 功能描述：定义返回类型 作 者： 王田朝 时 间： 2018年1月27日下午4:10:00
 * 
 * @param <T>
 */
public class ResultData<T> implements Serializable {
	/**
	 * 字段描述： 作 者： 王田朝 时 间： 2018年1月29日上午9:23:01
	 */
	private static final long serialVersionUID = 7548175061372213998L;
	public static final int SUCCESS = 200;
	public static final int ERROR = 500;
	public static final String SUCCESSMSG = "操作成功";
	public static final String ERRORMEG = "操作失败";

	private int state;
	/** 错误消息 */
	private String message;
	/** 返回正确时候的数据 */
	private T data;

	public ResultData() {
	}

	public ResultData(String error) {
		state = ERROR;
		this.message = error;
	}

	public ResultData(Object data) {
		state = SUCCESS;
		this.data = (T) data;
	}

	public ResultData(Throwable e) {
		state = ERROR;
		message = e.getMessage();
	}

	public ResultData(int state, Throwable e) {
		this.state = state;
		this.message = e.getMessage();
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public ResultData requestSuccess(T data) {
		this.data = data;
		this.message = SUCCESSMSG;
		this.state = SUCCESS;
		return this;
	}
	public ResultData requestError(){
		this.data=null;
		this.message=ERRORMEG;
		this.state=ERROR;
		return this;
	}
	public ResultData requestError(String errorMsg){
		this.data=null;
		this.message=errorMsg;
		this.state=ERROR;
		return this;
	}
	public ResultData requestError(Object object){
		this.data= (T) object;
		this.state=ERROR;
		return this;
	}
	
	
	@Override
	public String toString() {
		return "JsonResult [state=" + state + ", message=" + message + ", data=" + data + "]";
	}

	public ResultData requestSuccess() {
		this.message = SUCCESSMSG;
		this.state = SUCCESS;
		return this;
	}
}
